/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MRP.ProductionPlan;

import entity.MRP.ProductionPlan.PlannedWeeklyDemand;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.MRP.ProductionPlan.GenerateMonthlyProductionPlanManagerBean.ColumnModel;
import session.stateless.MRP.ProductionPlan.WeeklyProductionPlanBeanLocal;

/**
 *
 * @author zhozho
 */
@ManagedBean(name="weeklyProductionPlan")
@ViewScoped
public class GenerateWeeklyProductionPlanManagerBean implements Serializable {

    @EJB
    WeeklyProductionPlanBeanLocal weeklyProductionPlanBeanLocal;
        
    private String month;
    private String week;
    private List<String> monthList = new ArrayList<String>();
    private List<String> weekList = new ArrayList<String>();
    private List<String> header = new ArrayList<String>();
    private List<ColumnModel> simpleColumns;
    private List<PlannedWeeklyDemand> database = new ArrayList<PlannedWeeklyDemand>();
    
    private List<DataObject> dataListA;
    private List<DataObject> dataListB;
    private List<DataObject> dataListC;
    private List<DataObject> dataListD;
    
    
    /** Creates a new instance of GenerateWeeklyProductionPlanManagerBean */
    public GenerateWeeklyProductionPlanManagerBean() {}

    public void initView(PhaseEvent event){
        if (month == null)
        {
            database.clear();
            month = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("month");            
        }
        if (month != null)
        {
            if (database.isEmpty())
            {
                database = weeklyProductionPlanBeanLocal.getPlannedWeeklyDemands(month);
            }
            if (database.isEmpty())
            {            
                database = weeklyProductionPlanBeanLocal.createPlannedWeeklyDemands(month);
            }          
        }
        createDynamicColumns();
    }
    
    
     private void createDynamicColumns() 
     {  
        boolean isOpenWeek = false;
        String period = database.get(database.size() - 1).getWeekPeriod();
        int firstDay = Integer.parseInt(period.substring(0, period.indexOf("/")));
        String temp = period.substring(period.indexOf("/") + 1);
        int lastDay = Integer.parseInt(temp.substring(temp.indexOf("-")+1, temp.indexOf("/")));
        if (lastDay - firstDay != 4)
        {
            isOpenWeek = true;
        }
        dataListA = new ArrayList<DataObject>();
        dataListB = new ArrayList<DataObject>();
        dataListC = new ArrayList<DataObject>();
        dataListD = new ArrayList<DataObject>();
        
        dataListA.add(gatherMonthlyDemand("A", isOpenWeek));
        dataListA.add(gatherWorkingDaysInWeek(isOpenWeek));
        dataListA.add(gatherWorkingDaysInMonth(isOpenWeek));
        dataListA.add(gatherWeeklyDemand("A", isOpenWeek));
        
        dataListB.add(gatherMonthlyDemand("B", isOpenWeek));
        dataListB.add(gatherWorkingDaysInWeek(isOpenWeek));
        dataListB.add(gatherWorkingDaysInMonth(isOpenWeek));
        dataListB.add(gatherWeeklyDemand("B", isOpenWeek));
        
        dataListC.add(gatherMonthlyDemand("C", isOpenWeek));
        dataListC.add(gatherWorkingDaysInWeek(isOpenWeek));
        dataListC.add(gatherWorkingDaysInMonth(isOpenWeek));
        dataListC.add(gatherWeeklyDemand("C", isOpenWeek));
        
        dataListD.add(gatherMonthlyDemand("D", isOpenWeek));
        dataListD.add(gatherWorkingDaysInWeek(isOpenWeek));
        dataListD.add(gatherWorkingDaysInMonth(isOpenWeek));
        dataListD.add(gatherWeeklyDemand("D", isOpenWeek));
        
        
        
        
        for (int i = 0; i < database.size(); ++i)
        {
            temp = "Week " + database.get(i).getWeekNumber().toString() + 
                    " (" + database.get(i).getWeekPeriod().toString() + ")";
            
            header.add(temp);
            weekList.add(database.get(i).getWeekPeriod().toString());
        }
        if (isOpenWeek)
        {
            temp = "Week " + database.get(database.size() - 1).getWeekNumber().toString() + 
                    " (" + "No data" + ")";
            
            header.add(temp);
        }
        
        
        simpleColumns = new ArrayList<ColumnModel>();
        simpleColumns.add(new ColumnModel("Demand Management", "type"));
        
        String[] weekMap = {"one", "two", "three", "four", "five", "six"};
        for (int i = 0; i < database.size(); ++i) {
            simpleColumns.add(new ColumnModel(header.get(i), weekMap[i]));
        }
        if (isOpenWeek)
        {
            simpleColumns.add(new ColumnModel(header.get(database.size()), weekMap[database.size()]));
        }
     }
     
     
     private DataObject gatherMonthlyDemand(String productName, boolean isOpenWeek)
     {
         String[] list = new String[6];
         for (int i = 0; i < database.size(); ++i)
         {
             list[i] = database.get(0).getPlannedMonthlyDemand().getProductionPlan(productName).toString();
             
             if (list[i] == null)
             {
                 list[i] = "-";
             }
         }
         if (isOpenWeek)
         {
             list[database.size()] = weeklyProductionPlanBeanLocal.getNextMonth(month).getProductionPlan(productName).toString();
             if(list[database.size()] == null)
             {
                 list[database.size()] = "No data";
             }             
         }
         
         DataObject result = new DataObject("Monthly Demand", list[0], list[1], list[2], list[3], list[4], list[5]);
         return result;
     }
     
     private DataObject gatherWorkingDaysInWeek(boolean isOpenWeek)
     {
         String[] list = new String[6];
         for (int i = 0; i < database.size(); i++)
         {
             list[i] = database.get(i).getNumberOfWorkingDay().toString();
             if (list[i] == null)
             {
                 list[i] = "-";
             }
         } 
         if(isOpenWeek)
         {
             List<PlannedWeeklyDemand> firstWeek = new ArrayList<PlannedWeeklyDemand>();
             firstWeek = weeklyProductionPlanBeanLocal.getNextMonth(month).getListOfPlannedWeeklyDemand();
             if (!firstWeek.isEmpty())
             {
                list[database.size()] = firstWeek.get(0).getNumberOfWorkingDay().toString();
             }
             if(list[database.size()] == null)
             {
                 list[database.size()] = "No data";
             }
         }
         DataObject result = new DataObject("Working Days in Week", list[0], list[1], list[2], list[3], list[4], list[5]);
         return result;
     }
     
     private DataObject gatherWorkingDaysInMonth(boolean isOpenWeek)
     {
        String[] list = new String[6];
        for (int i = 0; i < database.size(); ++i)
        {
            if (month != null)
            {
                list[i] = Long.toString(database.get(i).getPlannedMonthlyDemand().getNumberOfWorkingDays());
            }
        }
        if (isOpenWeek)
        {
            list[database.size()] = Long.toString(weeklyProductionPlanBeanLocal.getNextMonth(month).getNumberOfWorkingDays());
            if(list[database.size()] == null)
             {
                 list[database.size()] = "No data";
             }
        }
        DataObject result = new DataObject("Working Days in Month", list[0], list[1], list[2], list[3], list[4], list[5]);
        return result;
     }
     
     public DataObject gatherWeeklyDemand(String productName, boolean isOpenWeek)
     {
         String[] list = new String[6];
         for (int i = 0; i < database.size(); ++i)
         {
            list[i] = database.get(i).getWeeklyDemand(productName).toString();
         }
         if (isOpenWeek)
         {
             List<PlannedWeeklyDemand> firstWeek = new ArrayList<PlannedWeeklyDemand>();
             firstWeek = weeklyProductionPlanBeanLocal.getNextMonth(month).getListOfPlannedWeeklyDemand();
             if (!firstWeek.isEmpty())
             {
                list[database.size()] = firstWeek.get(0).getWeeklyDemand(productName).toString();
             }
             if(list[database.size()] == null)
             {
                 list[database.size()] = "No data";
             }
         }
         DataObject result = new DataObject("Weekly Demand", list[0], list[1], list[2], list[3], list[4], list[5]);
         return result;
     }
     
     public void generateDaily(ActionEvent event) throws IOException
    {
        //week = "5/3-9/3";
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("week", week);
        FacesContext.getCurrentInstance().getExternalContext().redirect("generateDailyProductionPlan.xhtml");
    }
     
     static public class DataObject implements Serializable{
        private String type;
        private String one;
        private String two;
        private String three;
        private String four;
        private String five;
        private String six;


        public DataObject(String type, String one, String two, String three, String four, String five, String six) {

            this.type = type;
            this.one = one;
            this.two = two;
            this.three = three;
            this.four = four;
            this.five = five;
            this.six = six;
        }

        public String getFive() {
            return five;
        }

        public void setFive(String five) {
            this.five = five;
        }

        public String getFour() {
            return four;
        }

        public void setFour(String four) {
            this.four = four;
        }

        public String getOne() {
            return one;
        }

        public void setOne(String one) {
            this.one = one;
        }

        public String getSix() {
            return six;
        }

        public void setSix(String six) {
            this.six = six;
        }

        public String getThree() {
            return three;
        }

        public void setThree(String three) {
            this.three = three;
        }

        public String getTwo() {
            return two;
        }

        public void setTwo(String two) {
            this.two = two;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
        
        
    }

    public List<DataObject> getDataListA() {
        return dataListA;
    }

    public void setDataListA(List<DataObject> dataListA) {
        this.dataListA = dataListA;
    }

    public List<DataObject> getDataListB() {
        return dataListB;
    }

    public void setDataListB(List<DataObject> dataListB) {
        this.dataListB = dataListB;
    }

    public List<DataObject> getDataListC() {
        return dataListC;
    }

    public void setDataListC(List<DataObject> dataListC) {
        this.dataListC = dataListC;
    }

    public List<DataObject> getDataListD() {
        return dataListD;
    }

    public void setDataListD(List<DataObject> dataListD) {
        this.dataListD = dataListD;
    }

    public List<String> getMonthList() {
        return monthList;
    }

    public void setMonthList(List<String> monthList) {
        this.monthList = monthList;
    }

    public List<ColumnModel> getSimpleColumns() {
        return simpleColumns;
    }

    public void setSimpleColumns(List<ColumnModel> simpleColumns) {
        this.simpleColumns = simpleColumns;
    }

    public List<PlannedWeeklyDemand> getDatabase() {
        return database;
    }

    public void setDatabase(List<PlannedWeeklyDemand> database) {
        this.database = database;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getWeek() {
        return week;
    }

    public void setWeek(String week) {
        this.week = week;
    }

    public List<String> getWeekList() {
        return weekList;
    }

    public void setWeekList(List<String> weekList) {
        this.weekList = weekList;
    }

    public List<String> getHeader() {
        return header;
    }

    public void setHeader(List<String> header) {
        this.header = header;
    }
    
    
    
}
